package models

import (
	"github.com/gomodule/redigo/redis"
	"github.com/jinzhu/gorm"
)
type User struct {
	gorm.Model
	Username string `gorm:"size:20;unique;not null"`
	Password string `gorm:"size:20;not null"`
}

func ValidUser(name string,pwd string) (bool,User){
	var count int
	user := User{Username:name,Password:pwd}
	db.Where(&user).Find(&user).Count(&count)
	if count > 0 {
		return true,user
	}
	return false,User{}
}

func (u *User) SaveLoginLog(token string,ip string,mid string) error{
	conn,err := redis.Dial("tcp",dbConf.Cache.ConnStr["main"])
	if err != nil {
		return err
	}
	defer conn.Close()
	_,err = conn.Do("hmset",u.Username,"token",token,"ip",ip,"machineId",mid)
	if err != nil {
		return err
	}
	return nil
}
